//
//  UIBarButtonItem+Item.m
//  WSY微博
//
//  Created by tarena on 16/1/19.
//  Copyright © 2016年 WangSY. All rights reserved.
//

#import "UIBarButtonItem+Item.h"

@implementation UIBarButtonItem (Item)

+ (UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents title:(NSString *)title {
    // btn
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:highImage forState:UIControlStateHighlighted];
//    [btn sizeToFit];
    [btn setFrame:CGRectMake(0, 0,30, 44)];
    btn.titleLabel.font = FONT(15);
   
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn addTarget:target action:action forControlEvents:controlEvents];
    [btn sizeToFit];
    
    return  [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (UIBarButtonItem *)backBarButtonItemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents title:(NSString *)title {
    // btn
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:highImage forState:UIControlStateHighlighted];
    //    [btn sizeToFit];
    [btn setFrame:CGRectMake(0, 0, 50, 44)];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0,35,0,0)];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0,-20, 0,5)];
     btn.titleLabel.font = FONT(15);
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn addTarget:target action:action forControlEvents:controlEvents];
    
    return  [[UIBarButtonItem alloc] initWithCustomView:btn];
}


/**
 *  创建一个item
 *
 *  @param target    点击item后调用哪个对象的方法
 *  @param action    点击item后调用target的哪个方法
 *  @param image     图片
 *  @param highImage 高亮的图片
 *
 *  @return 创建完的item
 */

+ (UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets titleEdgeInsets:(UIEdgeInsets)titleEdgeInsets title:(NSString *)title {
    // btn
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [btn setImage:[highImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateHighlighted];
    [btn sizeToFit];
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [btn setImageEdgeInsets:imageEdgeInsets];
    [btn setTitleEdgeInsets:titleEdgeInsets];
    
    [btn setFrame:CGRectMake(0, 0, 70, 44)];
    
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return  [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (UIBarButtonItem *)barButtonItemWithBackgroundImage:(UIImage *)image highBackgroundImage:(UIImage *)highImage target:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents title:(NSString *)title {
    // btn
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:image forState:UIControlStateNormal];
    [btn setBackgroundImage:highImage forState:UIControlStateHighlighted];
    [btn sizeToFit];
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn addTarget:target action:action forControlEvents:controlEvents];
    
    return  [[UIBarButtonItem alloc] initWithCustomView:btn];
}



+ (UIBarButtonItem *)barButtonItemWithBackgroundImage:(UIImage *)image highBackgroundImage:(UIImage *)highImage target:(id)target action:(SEL)action title:(NSString *)title {
    // btn
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [btn setBackgroundImage:[highImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateHighlighted];
    [btn sizeToFit];
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return  [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (UIBarButtonItem *)LocationCityBarButtonItemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents title:(NSString *)title {
    // btn
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    //    leftView.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.font = FONT(15);
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:target action:action forControlEvents:controlEvents];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    CGFloat margin;
    if (title.length == 2) {
        [btn setFrame:CGRectMake(-10, 0, 50, 44)];
        margin = 6;
    } else if (title.length == 3) {
        [btn setFrame:CGRectMake(-10, 0, 60, 44)];
        margin = 4;
    } else if (title.length == 4) {
        [btn setFrame:CGRectMake(-10, 0, 70, 44)];
        margin = 2;
    } else {
        [btn setFrame:CGRectMake(-10, 0, 90, 44)];
        margin = 0;
    }
    margin = margin + 10;
    
    [leftView addSubview:btn];
    
    UIImageView *locationImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selectCity"]];
    
    [locationImageView setFrame:CGRectMake(btn.width - margin, 37 * 0.5, 10, 7)];
    [leftView addSubview:locationImageView];
    
    return  [[UIBarButtonItem alloc] initWithCustomView:leftView];
}

@end
